from sanic import Sanic
from config import config
from utils.myCache import Cache
from addict import Dict
from sanic_cors import CORS

# 初始化项目实例
app = Sanic(config.PROJECT_NAME)

# 默认设置可以跨域
CORS(app)

# 配置redis缓存组件, 前两项固定配置
if hasattr(config, 'REDIS_HOST'):
    cacheConfig = Dict()
    cacheConfig.serializer = "ujson"
    cacheConfig.mode = "sync"
    cacheConfig.default.engine = 'memory'
    cacheConfig.redis.host = config.REDIS_HOST
    cacheConfig.redis.port = config.REDIS_PORT
    cacheConfig.redis.db = config.REDIS_DB
    cacheConfig.redis.password = config.REDIS_PASSWORD
    app.ctx.cache = Cache(cacheConfig.to_dict())

# 配置引入蓝图url
from api import urls

app.blueprint(urls.routers)

if __name__ == '__main__':
    "docker run -itd --name playwright -p 8087:8087 playwright-utils-images /bin/bash /app/start.sh"
    import platform
    if platform.system().lower() == "windows":
        app.run(host='127.0.0.1', port=8087, debug=True, auto_reload=True, access_log=True)
    else:
        app.run(host='0.0.0.0', port=8087, workers=1)
